#include "datawidget.h"
#include "jointdatadialog.h"
#include <QCheckBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QScrollArea>
#include <QWheelEvent>
#include <QScrollBar>
#include <QMouseEvent>
#include <QApplication>
#include <QScreen>
#include <QDesktopWidget>
#include <QFont>
#include <QScroller>
#include <QHeaderView>
#include <QPainter>
#include "models/robotlist.h"

JointDataDialog::JointDataDialog(QWidget *parent)
    : QWidget(parent)
{
    setStyleSheet("QWidget{"
                  "background-color: #182B4C;"
                  "}");
    connect(RobotList::getInstance(), &RobotList::curRobotChanged, this, [&](Robot * pCurRobot, Robot * pLastRobot){
        if(pLastRobot)
            disconnect(pLastRobot,NULL, this,NULL);
        if(pCurRobot)
        {
            setupUI();
            connect(pCurRobot,&Robot::sportStateChanged, this, &JointDataDialog::updateData,Qt::UniqueConnection);
        }
    } );

    m_pScrollArea= new QScrollArea(this);
}


void JointDataDialog::setupUI()
{
    QWidget * pLastWidget = m_pScrollArea->widget();
    if(pLastWidget)
        pLastWidget->deleteLater();
    // 创建可拖动的容器
    QWidget *dataContainer = new QWidget(this);
    QVBoxLayout *dataLayout = new QVBoxLayout(dataContainer);
    dataLayout->setMargin(10*g_xScale);
    dataLayout->setSpacing(30 * g_yScale);
    QWidget *hWidget =new QWidget(dataContainer);
    hWidget->setStyleSheet("background-color: transparent;");
    hWidget->setFixedHeight(240 * g_yScale);
    hWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    QHBoxLayout *hLayout = new QHBoxLayout(hWidget);
    hLayout->setContentsMargins(0, 0,0, 0);
    hLayout->setSpacing(12 * g_xScale);
    // 添加数据窗口
    QString tableStyle = QString(
        "QTableWidget {"
        "    background-color: transparent;"
        "    border: 1px solid #5D81B0;"
        "    color: white;"
        "    gridline-color: #5D81B0;"
        "    font:%1px \"微软雅黑\";"
        "    border-image: none;"
        "}"
        "QHeaderView::section {"
        "    background-color: transparent;"
        "    color: white;"
        "    padding: 5px;"
        "    border: 1px  #5D81B0;"
        "    font:%1px \"微软雅黑\";"
        "    border-image: none;"
        "}"
        "QTableWidget::item {"
        "    background-color: transparent;"
        "    border-image: none;"
        "    height: %2px;"
        "    font:%1px \"微软雅黑\";"
        "}"
        "QHeaderView {"
        "    background-color: transparent;"
        "    font:%1px \"微软雅黑\";"
        "    border-image: none;"
        "    height: %3px;"
        "}"
        "QHeaderView::section:horizontal {"
        "    background-color: transparent;"
        "    color: white;"
        "    border: 1px solid #5D81B0;"
        "    border-left: none;"
        "    border-top: none;"
        "    font:%1px \"微软雅黑\";"
        "    border-image: none;"
        "    height: %3px;"
        "}"
        "QScrollBar:vertical {"
        "    background-color: #2E2F31;"
        "    width: 10px;"
        "    margin: 0px;"
        "}"
        "QScrollBar::handle:vertical {"
        "    background-color: #3E3E40;"
        "    min-height: 20px;"
        "}"
        "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
        "    height: 0px;"
        "}").arg(int(18*g_yScale)).arg(int(25*g_yScale)).arg(int(40*g_yScale));

    for (int i=0;i<11;i++) {  //一共11个类别
        switch (i) {
        case ROBOT_STATUS:
        {
            //处理状态信息
            m_pTableWidget = new QTableWidget();
            m_pTableWidget->setFixedHeight(80 * g_yScale);
            m_pTableWidget->setStyleSheet(tableStyle);

            // 隐藏列表序号
            m_pTableWidget->verticalHeader()->setVisible(false);
            // 禁用水平表头的点击功能
            m_pTableWidget->horizontalHeader()->setSectionsClickable(false);   // 禁用表头点
            m_pTableWidget->horizontalHeader()->setFixedHeight(40 * g_yScale);           // 固定表头高度;
            QStringList headers;
            headers << "当前模式" << "陀螺仪状态" << "偏航角" << "俯冲角"<<"翻滚角"<<"充电状态"<<"低压状态"<<"高压状态";

            //<<"使能状态"<<"电池电量"<<"关节电机最高温度"<<"温度最高电机编号"<<"告警信息";
            m_pTableWidget->setColumnCount(headers.size());
            m_pTableWidget->setRowCount(1);
            m_pTableWidget->setHorizontalHeaderLabels(headers);
            // 设置列占满整个宽度
            m_pTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
            // 设置表内容高度
            m_pTableWidget->verticalHeader()->setDefaultSectionSize(40*g_yScale);
            dataLayout->addWidget(m_pTableWidget);
            m_pTableWidget->setSelectionMode(QAbstractItemView::NoSelection);
            m_pTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
            m_pTableWidget->setFocusPolicy(Qt::NoFocus);
            //dataContainer->addFilterToWidget(m_pTableWidget);
            break;
        }
        case HEAD_JOINT:
        case WAIST_JOINT:
        {
            DataWidget *dataWidget = new DataWidget(this);
            //设置背景颜色
            dataWidget->setColor("#00000000","#5D81B0 ");
            dataWidget->setJointType((JointType)i);
            //dataWidget->setupUI();//初始化页面
            // 设置固定高度，确保不小于150
            dataWidget->setFixedHeight(240 * g_yScale);
            hLayout->addWidget(dataWidget);
            if(!dataLayout->children().contains(hWidget))
                dataLayout->addWidget(hWidget);
            //dataContainer->addFilterToWidget(dataWidget);
            break;
        }
        case LEFT_ARM_JOINT:
        case RIGHT_ARM_JOINT:
        case LEFT_LEG_JOINT:
        case RIGHT_LEG_JOINT:
        {
            DataWidget *dataWidget = new DataWidget(this);
            //设置背景颜色
            dataWidget->setColor("#00000000","#5D81B0 ");
            dataWidget->setJointType((JointType)i);
            // dataWidget->setupUI();//初始化页面

            // 设置固定高度，确保不小于150
            dataWidget->setFixedHeight(240 * g_yScale);
            dataLayout->addWidget(dataWidget);
            //dataContainer->addFilterToWidget(dataWidget);
            break;
        }
        case LEFT_TIP_JOINT:
        case RIGHT_TIP_JOINT:
        {
            DataWidget *dataWidget = new DataWidget(this);
            //设置背景颜色
            dataWidget->setColor("#00000000","#5D81B0 ");
            dataWidget->setJointType((JointType)i);
            //dataWidget->setupUI();//初始化页面
            // 设置固定高度，确保不小于150
            dataWidget->setFixedHeight(80 * g_yScale);
            dataLayout->addWidget(dataWidget);
            // dataContainer->addFilterToWidget(dataWidget);
            break;
        }
        case LEFT_TOOL_JOINT:
            {
            Robot * pCurRobot = RobotList::getInstance()->getCurRobot();
            if(pCurRobot)
            {
                switch (pCurRobot->leftTool()) {
                case TOOL_HAND:
                    {
                    DataWidget *dataWidget = new DataWidget(this);
                    //设置背景颜色
                    dataWidget->setColor("#00000000","#5D81B0 ");
                    dataWidget->setJointType(LEFT_TOOL_HAND_JOINT);
                    dataWidget->setFixedHeight(160 * g_yScale);
                    dataLayout->addWidget(dataWidget);
                    }
                    break;
                case TOOL_GRIPPER:
                    {
                        DataWidget *dataWidget = new DataWidget(this);
                        //设置背景颜色
                        dataWidget->setColor("#00000000","#5D81B0 ");
                        dataWidget->setJointType(LEFT_TOOL_GRIPPER_JOINT);
                        dataWidget->setFixedHeight(160 * g_yScale);
                        dataLayout->addWidget(dataWidget);
                    }
                    break;
                case TOOL_SUCTION_CUP:
                    {
                        DataWidget *dataWidget = new DataWidget(this);
                        //设置背景颜色
                        dataWidget->setColor("#00000000","#5D81B0 ");
                        dataWidget->setJointType(LEFT_TOOL_SUCKER_JOINT);
                        dataWidget->setFixedHeight(160 * g_yScale);
                        dataLayout->addWidget(dataWidget);
                    }
                    break;
                default:
                    break;
                }
            }
            break;
        }
        case RIGHT_TOOL_JOINT:
        {
            Robot * pCurRobot = RobotList::getInstance()->getCurRobot();
            if(pCurRobot)
            {
                switch (pCurRobot->rightTool()) {
                case TOOL_HAND:
                    {
                    DataWidget *dataWidget = new DataWidget(this);
                    //设置背景颜色
                    dataWidget->setColor("#00000000","#5D81B0 ");
                    dataWidget->setJointType(RIGHT_TOOL_HAND_JOINT);
                    dataWidget->setFixedHeight(160 * g_yScale);
                    dataLayout->addWidget(dataWidget);
                    }
                    break;
                case TOOL_GRIPPER:
                    {
                        DataWidget *dataWidget = new DataWidget(this);
                        //设置背景颜色
                        dataWidget->setColor("#00000000","#5D81B0 ");
                        dataWidget->setJointType(RIGHT_TOOL_GRIPPER_JOINT);
                        dataWidget->setFixedHeight(160 * g_yScale);
                        dataLayout->addWidget(dataWidget);
                    }
                    break;
                case TOOL_SUCTION_CUP:
                    {
                        DataWidget *dataWidget = new DataWidget(this);
                        //设置背景颜色
                        dataWidget->setColor("#00000000","#5D81B0 ");
                        dataWidget->setJointType(RIGHT_TOOL_SUCKER_JOINT);
                        dataWidget->setFixedHeight(160 * g_yScale);
                        dataLayout->addWidget(dataWidget);
                    }
                    break;
                default:
                    break;
                }
            }
            break;
        }
        default:
            break;
        }
    }
    // 将容器部件设置到滚动区域中
    m_pScrollArea->setWidget(dataContainer);
    m_pScrollArea->setWidgetResizable(true);
    // 启用鼠标左键拖动滚动手势
    QScroller::grabGesture(m_pScrollArea->viewport(), QScroller::LeftMouseButtonGesture);

    // 设置滚动属性（可选）
    QScrollerProperties scrollerProperties = QScroller::scroller(m_pScrollArea->viewport())->scrollerProperties();
    scrollerProperties.setScrollMetric(QScrollerProperties::DragVelocitySmoothingFactor, 0.8);
    scrollerProperties.setScrollMetric(QScrollerProperties::MinimumVelocity, 0.1);
    QScroller::scroller(m_pScrollArea->viewport())->setScrollerProperties(scrollerProperties);
}

void JointDataDialog::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    if(m_pScrollArea)
        m_pScrollArea->setGeometry(0,0,width(),height());
}


// void JointDataDialog::showEvent(QShowEvent *event)
// {
//     Q_UNUSED(event);
//     Robot* pCurRobot=RobotList::getInstance()->getCurRobot();
//     if(pCurRobot/*&&m_bFirst*/)
//     {
//         //m_bFirst=false;
//         connect(pCurRobot,&Robot::sportStateChanged, this, &JointDataDialog::updateData,Qt::UniqueConnection);
//     }
// }

void JointDataDialog::updateData()
{
    //更新数据
    // // 更新所有数据窗口
    int nIndex=0;
    Robot *pCurRobot=RobotList::getInstance()->getCurRobot();
    if(pCurRobot &&m_pTableWidget)
    {
        SportState hexData = pCurRobot->SportStateData();
        QString itemText;
        //更新当前模式
        switch (hexData.curCtrMode) {
        case 0:
            itemText="遥控模式";
            break;
        case 33:
            itemText="人员跟随模式";
            break;
        case 66:
            itemText="自主机动模式";
            break;
        default:
            break;
        }
        QTableWidgetItem *curModelitem = new QTableWidgetItem(itemText);
        curModelitem->setTextAlignment(Qt::AlignCenter);
        m_pTableWidget->setItem(0, nIndex++, curModelitem);
        //更新陀螺仪状态
        QBrush itemColor;
        switch (hexData.gyroStat) {
        case 0:
            itemText="正常";
            itemColor=QBrush(QColor(Qt::green));
            break;
        case 33:
            itemText="接收异常";
            itemColor=QBrush(QColor(Qt::red));
            break;
        case 66:
            itemText="传输中断";
            itemColor=QBrush(QColor(Qt::red));
            break;
        default:
            break;
        }
        QTableWidgetItem *gyroItem = new QTableWidgetItem(itemText);
        gyroItem->setTextAlignment(Qt::AlignCenter);
        gyroItem->setForeground(itemColor);
        m_pTableWidget->setItem(0, nIndex++, gyroItem);
        //更新偏航角 俯冲角 翻滚角
        for(int i=0;i<3;i++)
        {
            itemText=QString::number(hexData.gyroAngle[i]*180/M_PI,'f',2) + "°";
            QTableWidgetItem *angleItem = new QTableWidgetItem(itemText);
            angleItem->setTextAlignment(Qt::AlignCenter);
            m_pTableWidget->setItem(0, nIndex++, angleItem);
        }

        //充电状态
        //低压状态
        //高压状态
        //使能状态
        QStringList strList0;
        strList0<<"未充电"<<"未上低压"<<"未上高压";
        QStringList strList1;
        strList1<<"充电中"<<"已上低压"<<"已上高压";
        for(int i=0;i<3;i++)
        {
            if(hexData.powerStat[i]==1)
            {
                itemText=strList1[i];
                itemColor=QBrush(QColor(Qt::red));
            }
            else{
                itemText=strList0[i];
                itemColor=QBrush(QColor(Qt::green));
            }
            QTableWidgetItem *statusItem = new QTableWidgetItem(itemText);
            statusItem->setTextAlignment(Qt::AlignCenter);
            statusItem->setForeground(itemColor);
            m_pTableWidget->setItem(0, nIndex++, statusItem);
        }
        // //电池电量
        // itemText=QString::number(hexData.batteryLevel,'f',2);
        // QTableWidgetItem *batteryLevelItem = new QTableWidgetItem(itemText);
        // batteryLevelItem->setTextAlignment(Qt::AlignCenter);
        // m_pTableWidget->setItem(0, nIndex++, batteryLevelItem);
        // //关节电机最高温度
        // //温度最高电机编码
        // //告警信息
    }
}


















